import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-08-25
 * Time: 16:10
 */
class IntCmp implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}
public class TestPriorityQueue {


    public static void main(String[] args) {
        PriorityQueue<Integer> q1 =new PriorityQueue<>();
        q1.offer(4);
        q1.offer(20);
        q1.offer(13);
        q1.offer(45);
        System.out.println(q1.toString());
        System.out.println(q1.size());

        ArrayList<Integer> arrayList1=new ArrayList<>();
        arrayList1.add(-1);
        arrayList1.add(-45);
        arrayList1.add(23);
        PriorityQueue<Integer> q2 =new PriorityQueue<>(arrayList1);//用一个集合来创建优先级队列
        System.out.println(q2.toString());
        System.out.println("这里是分割线");
        System.out.println(q2.size());
        System.out.println(q2.poll());
        System.out.println(q2.peek());//// 获取优先级最高的元素  堆顶的元素
        System.out.println(q2.isEmpty());
        System.out.println("这里是分割线");
        PriorityQueue<Integer> q3 =new PriorityQueue<>(new IntCmp());
        q3.offer(-1);
        q3.offer(-45);
        q3.offer(23);
        System.out.println(q3.toString());

    }



}

class Student {
    public  int age;
    public  String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }


    public static void main(String[] args) {
        Student s1=new Student(78,"wanger");
        Student s2=new Student(23,"wang");
        AgeComparator ageComparator=new AgeComparator();
        System.out.println(ageComparator.compare(s1, s2));
        NameComparator nameComparator=new NameComparator();
        System.out.println(nameComparator.compare(s1, s2));

    }
}
class AgeComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
class NameComparator implements Comparator<Student>{
    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}



class Solution{
    @Override
    public String toString() {
        return "Solution{}";
    }

    public static int[] smallestK1(int[] array, int k){
        PriorityQueue<Integer> minHeap=new PriorityQueue<>();
        //生成一个小根堆
        for (int x: array) {
            minHeap.offer(x);
        }
        int[] ret=new int[k];
        //将前k个放置到一个数组中
        for (int i = 0; i <k; i++) {
            ret[i]=minHeap.poll();
        }
        return ret;
    }

    public static void main1(String[] args) {
        int[] array=new int[]{4,20,19,45};
        int[] ret=smallestK(array,3);
        System.out.println(Arrays.toString(ret));
    }

    class Imp implements Comparator<Integer>{
        @Override
        public int compare(Integer o1, Integer o2) {
            return o2-o1;
        }
    }
    public static int[] smallestK(int[] array, int k){
        PriorityQueue<Integer> maxHeap=new PriorityQueue<>(k,new IntCmp());
        //生成一个含有k个元素的大根堆
        for (int i = 0; i <k; i++) {
            maxHeap.offer(array[i]);
        }
        //剩下的元素依次和大根堆的根比较，如果比根小，就poll掉根，然后把这个元素加到堆中
        for (int i =k; i < array.length; i++) {
            int top=maxHeap.peek();
            if(top>array[i]){
                maxHeap.poll();
                maxHeap.offer(array[i]);
            }
        }
        int[] ret=new int[k];
        for (int i = 0; i <k; i++) {
            ret[i]= maxHeap.poll();
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] array=new int[]{4,20,19,45};
        int[] ret=smallestK(array,3);
        System.out.println(Arrays.toString(ret));
    }
}



